#include "widget.h"
#include "ui_widget.h"
#include<QTextCodec>
#include<QTimer>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    network_shopinf = new QNetworkAccessManager();
    network_shopinfreq = new QNetworkRequest();
    connect(network_shopinf, &QNetworkAccessManager::finished, this, &Widget::replyFinished);

QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, [&]{
      getshopinftosend();
});
    timer->start(5000);
}


void Widget::replyFinished(QNetworkReply *reply){
    //获取响应的信息，状态码为200表示正常
    QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    //无错误返回
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray bytes = reply->readAll();  //获取字节
        QString result(bytes);  //转化为字符串
        QTextCodec *t = QTextCodec::codecForName("UTF8");
        QByteArray ba = bytes;
        QString jsons =t->toUnicode(ba);
        qDebug()<<"begin";
        jsontoshopinf(jsons);  //开始解析数据
    }
    else
    {
        //处理错误
        qDebug()<<"处理错误";
    }
}

void Widget::jsontoshopinf(QString json)  //开始解析数据
{
    qDebug()<<"搜索解析";
    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(json.toUtf8(), &json_error);
    if(json_error.error == QJsonParseError::NoError){
        QJsonObject rootObj = parse_doucment.object();
        if(rootObj.contains("Date")){
            QJsonValue valuedata = rootObj.value("Date");
            QJsonArray heeloArray = valuedata.toArray();
            QVector<listmun> menulist;
            listmun menu;

            for (int i = 0; i < heeloArray.size(); i++){
                QJsonValue helloArrayValue = heeloArray.at(i);
                if(helloArrayValue.isObject()){
                    QJsonObject jsobj = helloArrayValue.toObject();
                    /*
        QString ischick;
        QString time;
        QString number;
        QString status;
*/
                    if(jsobj.contains("ischick"))
                    {
                        QJsonValue album_id = jsobj.take("ischick");
                        menu.ischick = album_id.toString();
                    }
                    if(jsobj.contains("time"))
                    {
                        QJsonValue album_name = jsobj.take("time");
                        menu.time = album_name.toString();
                    }
                    if(jsobj.contains("number"))
                    {
                        QJsonValue album_hot = jsobj.take("number");
                        menu.number = album_hot.toString();
                    }
                    if(jsobj.contains("status"))
                    {
                        QJsonValue album_minet = jsobj.take("status");
                        menu.status = album_minet.toString();
                    }

                }
                menulist.append(menu);
            }
            showplay(menulist);

        }

    }

}


void Widget::getshopinftosend(){
    QString KGAPISTR1 = QString("http://127.0.0.1:9000/list");
    network_shopinfreq->setUrl(QUrl(KGAPISTR1));
    network_shopinf->get(*network_shopinfreq);
}

void Widget::showplay(QVector<listmun>& lm){
    ui->listWidget->clear();
    ui->listWidget_2->clear();
    QVector<listmun>::iterator it = lm.begin();
    for(it;it!=lm.end();it++){
        if(it->ischick=="11"){
            QListWidgetItem *add_item = new QListWidgetItem(QIcon(":/new/prefix1/img/nb-.png"),it->number);
            ui->listWidget->addItem(add_item);
        }
        else if(it->ischick=="33"){
            if(it->status=="1"){
                QListWidgetItem *add_item = new QListWidgetItem(QIcon(":/new/prefix1/img/wancheng.png"),it->number);
                ui->listWidget_2->addItem(add_item);
            }
            else if(it->status=="2"){
                QListWidgetItem *add_item = new QListWidgetItem(QIcon(":/new/prefix1/img/peisongyue.png"),it->number);
                ui->listWidget_2->addItem(add_item);
            }
        }

    }


}

Widget::~Widget()
{
    delete ui;
}

